﻿using System;
using System.Collections.Generic;
using FriendlyURL.Web.Domain.Abstract;
using FriendlyURL.Web.Domain.Models;
using System.Linq;

namespace FriendlyURL.Web.Domain.Repositories
{
    public class LinkRepository : ILinkRepository
    {
        public Link GetLinkByLinkID(int linkId)
        {
            using (FriendlyURLEntities db = new FriendlyURLEntities())
            {
                var result = from j in db.Links where (j.LinkID.Equals(linkId)) select j;

                if (result.Count() != 0)
                {
                    var dblink = result.FirstOrDefault();

                    return dblink;
                }
                else
                {
                    return null;
                }
            }
        }

        public Link GetLinkByOwnerID(int ownerId)
        {
            using (FriendlyURLEntities db = new FriendlyURLEntities())
            {
                var result = from h in db.Links where (h.OwnerID.Equals(ownerId)) select h;

                if (result.Count() != 0)
                {
                    var dblink = result.FirstOrDefault();

                    return dblink;
                }
                else
                {
                    return null;
                }
            }
        }

        public Link GetLinkByShortURL(string shortURL)
        {
            using (FriendlyURLEntities db = new FriendlyURLEntities())
            {
                var result = (from k in db.Links where (k.ShortUrl.Equals(shortURL)) select k);

                if (result.Count() != 0)
                {
                    var dblink = result.FirstOrDefault();

                    return dblink;
                }
                else
                {
                    return null;
                }
            }
        }

        public IEnumerable<Link> GetLinks
        {
            get 
            {
                using (FriendlyURLEntities db = new FriendlyURLEntities())
                {
                    var result = (from allLinks in db.Links select allLinks).ToList<Link>();

                    if (result.Count() != 0)
                    {
                        return result;
                    }
                    else
                    {
                        return null;
                    }
                }            
            }
        }

        public bool IsValidLink(string shortURL)
        {
            return LinkExists(GetLinkByShortURL(shortURL));
        }

        public bool LinkExists(Link link)
        {
            bool exists = false;

            if (link != null)
            {
                exists = true;
            }
            
            return exists;
        }

        public bool LinkExists(int linkId)
        {
            return LinkExists(GetLinkByLinkID(linkId));
        }

        public Link CreateLink(string shortURL, string longURL, string description, DateTime createDate, int createdBy, DateTime modifiedDate, int modifiedBy, int ownerID, bool isActive, out AliasCreateStatus status)
        {
            try
            {
                using (FriendlyURLEntities db = new FriendlyURLEntities())
                {
                    if (IsValidLink(shortURL))
                    {
                        status = AliasCreateStatus.DuplicateAliasName;
                    }
                    else
                    {
                        Link link = new Link();

                        link.Active = isActive;
                        link.Created_By = createdBy;
                        link.Created_Date = createDate;
                        link.Description = description;
                        link.LongUrl = longURL;
                        link.Modified_By = modifiedBy;
                        link.Modified_Date = modifiedDate;
                        link.OwnerID = ownerID;
                        link.ShortUrl = shortURL;

                        db.AddToLinks(link);
                        db.SaveChanges();

                        status = AliasCreateStatus.Success;
                    }
                }
            }
            catch (Exception ex)
            {
                status = AliasCreateStatus.Error;
            }

            return GetLinkByShortURL(shortURL);
        }

        public Link UpdateLink(int linkId, string columnName, string value)
        {
            throw new NotImplementedException();
        }

        public bool DeleteLink(int linkId)
        {
            using (FriendlyURLEntities db = new FriendlyURLEntities())
            {
                var result = (from allLinks in db.Links where allLinks.LinkID.Equals(linkId) select allLinks).FirstOrDefault();

                if (result != null)
                {
                    db.Links.DeleteObject(result);
                    db.SaveChanges();
                }

                return LinkExists(linkId);
            }
        }
    }
}
